package com.dongdongshop.controller;

import com.dongdongshop.data.Result;
import com.dongdongshop.data.ResultEnum;
import com.dongdongshop.pojo.TbItemCat;
import com.dongdongshop.pojo.TbTypeTemplate;
import com.dongdongshop.service.ItemCatService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("itemCat")
public class ItemCatController {

    @DubboReference
    private ItemCatService itemCatService;

    @RequestMapping("itemCatList")
    public String itemCatList(Model model) {
        List<TbTypeTemplate> tl = itemCatService.itemCatList();
        model.addAttribute("tl",tl);
        return "/admin/item_cat";
    }

    @RequestMapping("getItemInfo")
    @ResponseBody
    public Result getItemInfo(Long id) {
        try {
            List<TbItemCat> tbItemCats = itemCatService.getItemInfo(id);
            return Result.ok().setData(tbItemCats);
        }catch (Exception e) {
            return Result.respones(ResultEnum.ERROR);
        }
    }

    @RequestMapping("addItem")
    @ResponseBody
    public Result addItem(TbItemCat tbItemCat) {
        try {
            itemCatService.addItem(tbItemCat);
            return Result.respones(ResultEnum.SUCCESS);
        }catch (Exception e) {
            return Result.respones(ResultEnum.ERROR);
        }
    }

    @RequestMapping("updateById")
    @ResponseBody
    public Result getOneItemInfo(Long id) {
        try {
            TbItemCat tbItemCat = itemCatService.updateById(id);
            return Result.ok().setData(tbItemCat);
        }catch (Exception e) {
            return Result.respones(ResultEnum.ERROR);
        }
    }

    @RequestMapping("updateItem")
    @ResponseBody
    public Result updateItem(TbItemCat tbItemCat) {
        try {
            itemCatService.updateItem(tbItemCat);
            return Result.respones(ResultEnum.SUCCESS);
        }catch (Exception e) {
            return Result.respones(ResultEnum.ERROR);
        }
    }

    @RequestMapping("deleteItem")
    @ResponseBody
    public Result deleteItem(Long[] id) {
        try {
            itemCatService.deleteItem(id);
            return Result.respones(ResultEnum.SUCCESS);
        }catch (Exception e) {
            return Result.respones(ResultEnum.ERROR);
        }
    }


}
